package coller

import (
	"github.com/gin-gonic/gin"
	"model"
	"net/http"
	"strconv"
	"types"
)

// 课程添加
func ClassAdd(c *gin.Context) {
	UserId, _ := strconv.Atoi(c.PostForm("user_id"))
	SeaId, _ := strconv.Atoi(c.PostForm("sea_id"))
	Name := c.PostForm("name")
	ClassTime := c.PostForm("class_time")
	Price, _ := strconv.Atoi(c.PostForm("price"))
	New, _ := strconv.Atoi(c.PostForm("new"))
	Max, _ := strconv.Atoi(c.PostForm("max"))
	pone := model.Class{
		UserId:    UserId,
		SeaId:     SeaId,
		Name:      Name,
		ClassTime: ClassTime,
		Price:     float64(Price),
		New:       New,
		Max:       Max,
	}
	err := pone.ClassAdd()
	if err != nil {
		c.JSON(http.StatusBadRequest, types.Router{
			Code: -1,
			Msg:  "课程添加失败",
			Data: nil,
		})
		return
	}
	//timer2 := time.NewTimer(100 * time.Second)
	//for {
	//	<-timer2.C
	//	fmt.Println("注册失败")
	//}
	c.JSON(http.StatusOK, types.Router{
		Code: 200,
		Msg:  "添加成功",
		Data: nil,
	})
}

// 课程列表
func ClassList(c *gin.Context) {
	poen := model.Class{}
	res, err := poen.ClasList()
	if err != nil {
		c.JSON(http.StatusBadRequest, types.Router{
			Code: -1,
			Msg:  "列表展示失败",
			Data: nil,
		})
		return
	}
	c.JSON(http.StatusOK, types.Router{
		Code: 200,
		Msg:  "列表展示成功",
		Data: map[string]interface{}{
			"res": res,
		},
	})
}

// 课程实时更新
func ClassUpdate(c *gin.Context) {
	Name := c.PostForm("name")
	pone := model.Class{Name: Name}
	update, err := pone.ClassUpdate()
	if err != nil {
		c.JSON(http.StatusBadRequest, types.Router{
			Code: -1,
			Msg:  "替换失败",
			Data: nil,
		})
		return
	}
	c.JSON(http.StatusOK, types.Router{
		Code: 200,
		Msg:  "替换成功",
		Data: map[string]interface{}{
			"Update": update,
		},
	})
}

// 统计
func ClassCounter(c *gin.Context) {
	Name := c.PostForm("name")
	pone := model.Class{Name: Name}
	_, err := pone.ClassCounter()
	if err != nil {
		c.JSON(http.StatusBadRequest, types.Router{
			Code: -1,
			Msg:  "查询失败",
			Data: nil,
		})
		return
	}

	c.JSON(http.StatusOK, types.Router{
		Code: 200,
		Msg:  "查询成功",
		Data: map[string]interface{}{
			"counter":  18,
			"NumPrice": 18 * 21,
		},
	})
}
